#include "stdafx.h"
#include "DisplayStyle.h"
#include "Claxa.h"

//#include <MyControls/PropertyGroup.h>
#include <MyControls/PropertyString.h>

bool operator == (const DisplayStyle & l, const DisplayStyle & r)
{
	if (l.Type != r.Type)
		return false;

	if (l.Type == ObjectOnly ||
		l.Type == BackgroundOnly)
		return true;

	if (l.SmoothMask != r.SmoothMask ||
		l.InverseMask != r.InverseMask)
		return false;

	if (l.Type == MattedObject &&
		l.MatteColor != r.MatteColor)
		return false;

	return true;
}

inline byte_color int2color(int c)
	{ return reinterpret_cast<byte_color&>(c); }

inline int color2int(byte_color c)
	{ return reinterpret_cast<const int&>(c); }

void DisplayStyle::WriteSettings()
{
	theApp.WriteProfileInt("Display", "Type", Type);
	theApp.WriteProfileInt("Display", "SmoothMask", SmoothMask ? 1 : 0);
	theApp.WriteProfileInt("Display", "InverseMask", InverseMask ? 1 : 0);
	theApp.WriteProfileInt("Display", "MatteColor",	color2int(MatteColor));
}

void DisplayStyle::ReadSettings()
{
	Type = (DisplayType)theApp.GetProfileInt("Display", "Type", Composition);
	if ((unsigned)Type > Composition)
		Type = Composition;

	SmoothMask = theApp.GetProfileInt("Display", "SmoothMask", 1) != 0;
	InverseMask = theApp.GetProfileInt("Display", "InverseMask", 1) != 0;
	MatteColor = int2color(theApp.GetProfileInt("Display", "MatteColor", color2int(ByteColor(255,0,0,128))));
}

template <>
My::EnumItem<DisplayType> My::EnumTypeControl<DisplayType>::Items[] =
{
	{"Mask Only (1)", MaskOnly},
	{"Foreground Only (2)", ObjectOnly},
	{"Background Only (3)", BackgroundOnly},
	{"Matted Foreground (4)", MattedObject},
	{"Composition (5)", Composition}
};

SPECIALIZE_ENUM_TYPE_CONTROL(DisplayType)

My::Property<DisplayStyle>::Property() :
	StructProperty<DisplayStyle>(true)
{
}

void My::Property<DisplayStyle>::Init()
{
	Read(CXView::GetDisplayStyle).Write(CXView::SetDisplayStyle);

	(* this)[IDS_DISPLAY_TYPE] = &Create(&DisplayStyle::Type);

	//static My::PropertyGroup Mask("");
	static My::PropertyString Mask("");
	(* this)[IDS_DISPLAY_MASK] = &Mask;
	Mask[IDS_DISPLAY_MASK_SMOOTH] = &Create(&DisplayStyle::SmoothMask).ReadOnly(My::Read<bool>(* this, DisplayStyle::MaskStyleDisabled));
	Mask[IDS_DISPLAY_MASK_INVERSE] = &Create(&DisplayStyle::InverseMask).ReadOnly(My::Read<bool>(* this, DisplayStyle::MaskStyleDisabled));

	//static My::PropertyGroup Matte("");
	static My::PropertyString Matte("");
	(* this)[IDS_DISPLAY_MATTE] = &Matte;
	Matte[IDS_DISPLAY_MATTE_COLOR] = &Create(&DisplayStyle::MatteColor).ReadOnly(My::Read<bool>(* this, DisplayStyle::MatteStyleDisabled));
	Matte[IDS_DISPLAY_MATTE_OPACITY] = &Create(My::ReadWrite< My::Percent<> >(* this, &DisplayStyle::GetMatteOpacity, &DisplayStyle::SetMatteOpacity)).
		ReadOnly(My::Read<bool>(* this, DisplayStyle::MatteStyleDisabled));
}

